clear
#delimit;
set scheme s2color;
cap log close;
use "${datapath}\member_d.dta"; 


eststo clear;
qui eststo:ivregress 2sls water2 (res_w=random) gp1-gp50,robust ;
qui estadd ysumm;
foreach x in  stret road drain {;
eststo:qui ivregress 2sls `x'ju2015 (res_w=random) gp1-gp50,robust ;
qui estadd ysumm;
};

esttab, b(3) t(2) keep(res_w) se  compress mtitles("water" "Lights" "Road"  "Drainage") stats(ymean N, labels 
 ("Mean" "Observations") fmt(2 0));
esttab using "${outpath}\mem_public.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("water""Lights" "Road"  "Drainage") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

/* election */;
replace getvote="" if getvote=="Uncontested";
destring getvote opon1vote,replace;
gen voteshare=getvot/totolvot;
gen mergine=(getvot-opon1vote)/totolvot;
gen uncontest=1 if getvote==.;
replace uncontest=0 if uncontest==.;

* vote share and mergine cannot be estimated by IV;
foreach x in voteshare mergine  {;
eststo:qui regress `x' res_w gp1-gp50,robust ;
qui estadd ysumm;
};
foreach x in fiednomi uncontest  {;
eststo:qui ivregress 2sls `x' (res_w=random) gp1-gp50,robust ;
qui estadd ysumm;
};
esttab, b(3) t(2) keep(res_w) se  compress mtitles("voteshare" "mergine" "contest" "uncontest" "expenditure""nextjob") stats(ymean N, labels 
 ("Mean" "Observations")  fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\election.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("voteshare" "mergine" "contest"
 "uncontest" "expenditure""nextjob") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01) fragment;
eststo clear;

replace tnwginc=tnwginc/1000;
foreach x in educ agecom1 exp_self tnwginc totland ans_mem{;
eststo:qui ivregress 2sls `x' (res_w=random) gp1-gp50,robust ;
qui estadd ysumm;
};
esttab, b(3) t(2) keep(res_w) se  compress mtitles("educ" "agecom1" "exp_self" "income" "totland") stats(ymean N, labels 
 ("Mean" "Observations")  fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\affiliation.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles("educ" "agecom1" "exp_self" "income" "totland") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05  ** 0.01) fragment;
eststo clear;


* Analysis using incumbent data *;


eststo:qui ivregress 2sls stand2020_f (res_w=random) gp1-gp50,robust ;

esttab, b(3) t(2) keep(res_w) se  compress mtitles(  "stand2020_f") stats(ymean N, labels 
 ("Mean" "Observations")  fmt(2 0)) star(+ 0.10 * 0.05 ** 0.01);
esttab using "${outpath}\incumbent1.csv", replace label b(3) t(2) keep(res_w) se  compress mtitles( "stand2020_f") stats(ymean N, labels ("Mean" "Observations") fmt(2 0)) star(+ 0.10 * 0.05  ** 0.01) fragment;
eststo clear;



